package com.uzhie.web.aop;

import com.alibaba.fastjson.JSONObject;
import com.uzhie.spring.HttpServletRequestWrapper;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by TangFei on 2018/8/15.
 * 参数处理拦截器
 * 剥离请求参数中的 requestBody
 * 封装返回参数结果 responseBody
 */
public class ParamInterceptor implements HandlerInterceptor {

    private Logger logger = Logger.getLogger(getClass());

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        logger.trace("enter ParamInterceptor method preHandle");
        HttpServletRequestWrapper requestWrapper = (HttpServletRequestWrapper) httpServletRequest;
        JSONObject requestBody = requestWrapper.getRequestJSON().getJSONObject("requestBody");
        requestWrapper.getRequestJSON().fluentClear().putAll(requestBody);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        logger.trace("enter ParamInterceptor method postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        logger.trace("enter ParamInterceptor method afterCompletion");
    }
}
